import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
/*登陆拦截*/
/*import Mock from './mock'
Mock.bootstrap();*/
import 'font-awesome/css/font-awesome.min.css'
//配置axios
import axios from "axios";
//配置axios全局路径
axios.defaults.baseURL='http://localhost:80/';
// axios.defaults.baseURL='http://localhost:8080/';
// axios.defaults.baseURL='http://localhost:80/api';
// axios.defaults.baseURL='/api';//这里默认就是前端 可以省略http://localhost:80
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})
/*axios拦截 所有请求都携带token*/
axios.interceptors.request.use(config=>{
  let token =  JSON.parse(localStorage.getItem("token"))//localStorage中存的是json字符串 需要转化成json对象
  console.log("token:"+token)
  if(token){
    config.headers['token']=token;
  }
  return config;
},error => {
  Promise.reject(error);
})
//使用axios后置拦截器，处理没有登录请求
//==============axios后台拦截器实现后端已经退出登录的跳转登录页面======================//
axios.interceptors.response.use(config=>{
  let data = config.data;
  if(!data.success && "noLogin"===data.message)
  {
    //没有登陆就跳转到登陆界面
    localStorage.removeItem('loginUser');
    localStorage.removeItem('token');
    router.push({path: '/login'});
  }
  return config;
},error => {
  Promise.reject(error)
})
//==============axios后台拦截器实现后端已经退出登录的跳转登录页面======================//

/*登陆拦截*/
router.beforeEach((to, from, next) => {
  //NProgress.start();
  if (to.path == '/login') {
    localStorage.removeItem('user');
  }
  let user = JSON.parse(localStorage.getItem('loginUser'));
  if (!user && to.path != '/login' && to.path != '/shopEnter') {
    next({ path: '/login' })
  } else {
    next()
  }
})

//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

